//
//  GoodsDetailViewController.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/8/2.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "GoodsDetailViewController.h"
#import <UIImageView+WebCache.h>
//跳转界面
#import "SubmitOrderViewController.h"
#import "StandardsView.h"//商品规格选择

//自定义视图
#import "GoodsDetailTopView.h"//顶部视图
#import "BuyGoodsView.h"//底部视图
//#import "ChooseGoodTypeView.h"//选择商品规格

//自定义cell
//左边tableViewcell
#import "GoodsScrollTableViewCell.h"//顶上大图
#import "commentTableViewCell.h"//买家评论title
#import "CommentContentTableViewCell.h"//评论内容
#import "MoreBtnTableViewCell.h"//查看更多
#import "GoodsDeatilTableViewCell.h"//商品名称，价格，收藏数。。。。
//右边tableViewcell
#import "PromiseTableViewCell.h"//优汇聚承诺
#import "GoodInfoTitleTableViewCell.h"//商品信息
#import "GoodInfoTableViewCell.h"//商品信息内容
#import "GoodDetailImgTableViewCell.h"//商品详情图



//网络请求
#import "YORequestHander.h"
#import "goodModel.h"


//NSUserDefaults
#import "CacheUserInfo.h"
@interface GoodsDetailViewController ()<UITableViewDelegate,UITableViewDataSource,StandardsViewDelegate>

//@property (strong, nonatomic) UIScrollView *bottomScrollView;
@property (weak, nonatomic) IBOutlet UIScrollView *bottomScrollView;


@property(nonatomic,strong)GoodsDetailTopView *topView;
@property(nonatomic,strong)BuyGoodsView *buyfoodsView ;

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UITableView *detailTableView;
//@property(nonatomic,strong)UIScrollView *scrollView;
//@property(nonatomic,strong)UIButton *toTopButton;//回顶部

@property(nonatomic,strong)YORequestHander *requestHander;
@property(nonatomic,strong)NSArray *dataSource;//数据
@property(nonatomic,strong)NSArray *dateImg;//详情图

@property(nonatomic,strong)UIButton *toTopButton;//回顶部

//NSUserDefaults
@property(nonatomic,strong)CacheUserInfo *userInfo;

//占位数据
@property(nonatomic,assign)NSInteger flag;


@end

@implementation GoodsDetailViewController

static NSString *commentTableViewCellIden = @"commentTableViewCellIden";
static NSString *CommentContentTableViewCellIden = @"CommentContentTableViewCellIden";
static NSString *MoreBtnTableViewCellIden = @"MoreBtnTableViewCellIden";
static NSString *GoodsScrollTableViewCellIden = @"GoodsScrollTableViewCellIden";
static NSString *GoodsDeatilTableViewCellIden = @"GoodsDeatilTableViewCellIden";

static NSString *PromiseTableViewCellIden = @"PromiseTableViewCellIden";
static NSString *GoodInfoTitleTableViewCellIden = @"GoodInfoTitleTableViewCellIden";
static NSString *GoodInfoTableViewCellIden = @"GoodInfoTableViewCellIden";
static NSString *GoodDetailImgTableViewCellIden = @"GoodDetailImgTableViewCellIden";





- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self getData];
    
    [self load_UI];
    
    
    // Do any additional setup after loading the view from its nib.
}

#pragma mark ------ load_UI
-(void)load_UI
{

    
    self.flag = 0;
    
    [self topView];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
//    [self bottomScrollView];
    
//    self.bottomScrollView = [[UIScrollView alloc]init];
    self.bottomScrollView.backgroundColor = [UIColor whiteColor];
    self.bottomScrollView.showsHorizontalScrollIndicator = YES;
//    self.bottomScrollView.scrollEnabled = NO;
    self.bottomScrollView.pagingEnabled = YES;
//    self.bottomScrollView.contentSize = CGSizeMake(750 * FitW, self.bottomScrollView.frame.size.height);
    self.bottomScrollView.contentSize = CGSizeMake(750 * FitW, self.view.frame.size.height - 64);
//    [self.view addSubview:self.bottomScrollView];
//    [self.bottomScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
////        make.edges.mas_offset(UIEdgeInsetsMake(64, 0, 0, 0));
//                    make.left.equalTo(self.view.mas_left);
//                    make.top.equalTo(self.view.mas_top).offset(64);
//                    make.bottom.equalTo(self.view.mas_bottom);
//                    make.right.equalTo(self.view.mas_right);
//    }];

    
//    self.bottomScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height - 64);
//    self.bottomScrollView.contentOffset = CGPointMake(0,0);
    
    [self tableView];
    
    
    
    
    
    [self detailTableView];
    
//    _toTopButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    _toTopButton.backgroundColor = [UIColor clearColor];
//    [_toTopButton setBackgroundImage:LoadImage(@"toTop") forState:UIControlStateNormal];
//    [_toTopButton addTarget:self action:@selector(scrollToTopAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:_toTopButton];
//    [_toTopButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.view.mas_right).offset(-20 * FitW);
//        make.bottom.equalTo(self.view.mas_bottom).offset(-100 * FitH);
//        make.size.mas_equalTo(CGSizeMake(40 * FitW, 40 * FitH));
//    }];
//    _toTopButton.alpha = 0;
    
    //回顶部
    _toTopButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _toTopButton.backgroundColor = [UIColor clearColor];
    [_toTopButton setBackgroundImage:LoadImage(@"toTop") forState:UIControlStateNormal];
    [_toTopButton addTarget:self action:@selector(toTopAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.detailTableView addSubview:_toTopButton];
    [_toTopButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-20 * FitW);
        make.bottom.equalTo(self.view.mas_bottom).offset(-100 * FitH);
        make.size.mas_equalTo(CGSizeMake(40 * FitW, 40 * FitH));
    }];
    //    _toTopButton.hidden = YES;
    _toTopButton.alpha = 0;
    
}

#pragma mark ------ getData
-(void)getData
{
    [self.requestHander getWithAPIName:self.goodDetailAPI apiVersion:nil parameters:nil cachIdentifier:nil success:^(id response) {
       
//        DLog(@"请求成功%@",response);
        NSArray *models = response[@"price"];
        DLog(@"%@",models);
        self.dataSource = [[MTLJSONAdapter modelsOfClass:[GoodModel class] fromJSONArray:models error:nil] mutableCopy];
        [self.tableView reloadData];
        //获取详情页数据
//        [self getDetailImg];
    } failure:^(NSString *errorMessage) {
        DLog(@"请求失败");
    }];
}

//获取详情页数据
//-(void)getDetailImg
//{
//    
//    [self.requestHander getWithAPIName:[self.dataSource.lastObject good_detailImgAPI] apiVersion:nil parameters:nil cachIdentifier:nil success:^(id response) {
//        NSArray *models = response[@"goodsDetailInfo"];
//        self.dateImg = [[MTLJSONAdapter modelsOfClass:[GoodModel class] fromJSONArray:models error:nil] mutableCopy];
//        
//        UIImageView *showImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 351 * FitW, 2652 * FitH)];
//        //        showImage.image = LoadImage(@"商品详情页.jpg");
//        [showImage sd_setImageWithURL:[NSURL URLWithString:[self.dateImg.lastObject good_deatailImg]]];
//        
//        [_scrollView addSubview:showImage];
//        
//    } failure:^(NSString *errorMessage) {
//        
//    }];
//}


#pragma mark ------ tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableView){
//        return 2;
        return 1;
    }else{
        return 3;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
//        if (section == 0)
//        {
//            return 2;
//        }
//        else
//        {
//            
//            return 7;
//        }
        return 2;
    }else{
        if (section == 1) {
            return 1;
        }else if (section == 0){
            return 2 + 10;//2 + self.datearray.count
        }else{
            return 1;
        }
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView){
//        switch (indexPath.section)
//        {
//            case 0:
//            {
                if (indexPath.row == 0)
                {
                    return 355 * FitH;//商品大图
                }
                else
                {
                    return 220 * FitH;//商品名称，价格，收藏，销量等
//                }
//            }
//                break;
//            case 1:
//            {
//                if (indexPath.row == 0)//买家评论title
//                {
//                    return 70 * FitH;
//                }
//                else if (indexPath.row == 6)//最后一个cell(查看更多评论)
//                {
//                    return 58 * FitH;
//                }
//                else  //评论
//                {
//                    return 80 * FitH;
//                }
//            }
//                break;
//            default:
//                return 0;
        }
    }else{
        switch (indexPath.section)
        {
            case 1:
                return 279 * FitH;
                break;
            case 0:
                if (indexPath.row == 0 || indexPath.row == 11) {//3为2 + self.datearray.count - 1
                    return 56 * FitH;
                }else{
                    return 25 * FitH;
                }
                break;
            case 2:
            {
//                [self imgHeight:[[UIImage alloc]init] ];
                return [self imgHeight:[[UIImage alloc]init] ];
            }
                break;
            default:
                return 0;
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        switch (indexPath.section)
        {
            case 0:
            {
                if (indexPath.row == 0)//商品大图（可为轮播图）
                {
                    GoodsScrollTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodsScrollTableViewCellIden forIndexPath:indexPath];
                    
                    DLog(@"%@",self.dataSource.lastObject);
                    UIImageView *showImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
                    //                        [showImg sd_setImageWithURL:[NSURL URLWithString:[self.dataSource.lastObject good_img]]];
                    showImg.backgroundColor = [UIColor grayColor];
                    [cell addSubview:showImg];
                    
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    
                    
                    
                    
                    return cell;
                }
                else   //商品名称，价格，收藏，销量等
                {
                    GoodsDeatilTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodsDeatilTableViewCellIden forIndexPath:indexPath];
                    cell.model = self.dataSource.lastObject;
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return cell;
                }
            }
                break;
            case 1:
            {
                if (indexPath.row == 0)//买家评论title
                {
                    commentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:commentTableViewCellIden forIndexPath:indexPath];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return cell;
                }
                else if (indexPath.row == 6)  //查看更多评论
                {
                    MoreBtnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MoreBtnTableViewCellIden forIndexPath:indexPath];
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    cell.MoreButtonClickedHandler = ^(NSInteger buttonTag){
                        
                        DLog(@"更多评论");
                    };
                    return cell;
                }
                else  //评论
                {
                    CommentContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CommentContentTableViewCellIden forIndexPath:indexPath];
                    
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return cell;
                }
            }
                break;
                
            default:
                return nil;
        }
    }else{
        switch (indexPath.section)
        {
            case 1:
            {
                PromiseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PromiseTableViewCellIden forIndexPath:indexPath];
                return cell;
            }
                break;
            case 0:
            {
                if (indexPath.row == 0)
                {
                    GoodInfoTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodInfoTitleTableViewCellIden forIndexPath:indexPath];
                    cell.dateString = @"商品信息";
                    return cell;
                }else if (indexPath.row == 11){
                    GoodInfoTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodInfoTitleTableViewCellIden forIndexPath:indexPath];
                    cell.dateString = @"图文详情";
                    return cell;
                }else{
                    GoodInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodInfoTableViewCellIden forIndexPath:indexPath];
                    return cell;
                }
            }
                break;
            case 2:
            {
                GoodDetailImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:GoodDetailImgTableViewCellIden forIndexPath:indexPath];
                
//                UIImageView *testImg = [[UIImageView alloc]initWithFrame:cell.frame];
//                testImg.image = LoadImage(@"testttt");
//                [cell addSubview:testImg];
                
                return cell;
            }
                break;
            default:
                return nil;
        }
    }
    
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y > 700 * FitH)
    {
        //        self.toTopButton.hidden = NO;
        [UIView animateWithDuration:1 animations:^{
            
            self.toTopButton.alpha = 1;
        }];
        
    }
    else
    {
        [UIView animateWithDuration:1 animations:^{
            
            self.toTopButton.alpha = 0;
        }];
        
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark ------ 商品规格  代理方法

-(StandardsView *)buildStandardView:(UIImage *)img andIndex:(NSInteger)index
{
    StandardsView *standview = [[StandardsView alloc] init];
    standview.tag = index;
    standview.delegate = self;
    
    [standview.mainImgView sd_setImageWithURL:[NSURL URLWithString:[self.dataSource.lastObject good_img]]];
    standview.mainImgView.backgroundColor = [UIColor whiteColor];
//    standview.priceLab.text = @"¥ 108";
    standview.priceLab.text = [NSString stringWithFormat:@"¥ %@",[self.dataSource.lastObject good_price_onSale]];
//    standview.tipLab.text = @"请选择规格";
//    standview.goodNum.text = @"韩国正品Guerisson/九朵云马油..... ";
//    standview.goodNum.text = [NSString stringWithFormat:@"%@",[self.dataSource.lastObject good_name]];
    standview.tipLab.text = [NSString stringWithFormat:@"%@",[self.dataSource.lastObject good_name]];
    
//    standview.customBtns = @[@"确定"];
    
    NSArray *arr = @[@"100ml",@"200ml",@"300ml",@"400ml",@"500ml",@"600ml",@"700ml",@"800ml",@"900ml"];
//    NSArray *arr2 = @[@"1000",@"2000",@"3000",@"4000",@"5000",@"6000",@"7000",@"8000",@"9000"];
//    NSArray *arr3 = @[@"10",@"20",@"30",@"40",@"50",@"60",@"70",@"80",@"90"];
//    NSArray *arr4 = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
//    NSArray *allArr = @[arr,arr2,arr3,arr4];
    NSArray *allArr = @[arr];
    NSMutableArray *array = [NSMutableArray array];
    
    
    NSMutableArray *secArr = [NSMutableArray array];
//    NSArray *secArray = @[@"大小",@"重量",@"颜色",@"尺寸",];
    NSArray *secArray = @[@"产品规格"];

    
    for (int j = 0; j < allArr.count; j++)
    {
        
        for (int i = 0; i < [allArr[j] count]; i++)//规格参数
        {
            //        NSString *str = [NSString stringWithFormat:@"tempClassInfo%zd",i];
            standardClassInfo *tempClassInfo = [standardClassInfo StandardClassInfoWith:[NSString stringWithFormat:@"%zd",100 + i] andStandClassName:arr[i]];
            [array addObject:tempClassInfo];
        }
        
        for (int i = 0; i < secArray.count ; i++)//
        {
            StandardModel *tempModel = [StandardModel StandardModelWith:array andStandName:secArray[i]];
            [secArr addObject:tempModel];
        }

    }
    
    
//    StandardModel *tempModel = [StandardModel StandardModelWith:array andStandName:@"选择型号"];
    
//     standview.standardArr = secArr;
    
//    standardClassInfo *tempClassInfo1 = [standardClassInfo StandardClassInfoWith:@"100" andStandClassName:@"100ml"];
//    standardClassInfo *tempClassInfo2 = [standardClassInfo StandardClassInfoWith:@"101" andStandClassName:@"200ml"];
//    standardClassInfo *tempClassInfo3= [standardClassInfo StandardClassInfoWith:@"102" andStandClassName:@"300ml"];
//    standardClassInfo *tempClassInfo4 = [standardClassInfo StandardClassInfoWith:@"103" andStandClassName:@"400ml"];
//    standardClassInfo *tempClassInfo5 = [standardClassInfo StandardClassInfoWith:@"104" andStandClassName:@"500ml"];
//    standardClassInfo *tempClassInfo6 = [standardClassInfo StandardClassInfoWith:@"105" andStandClassName:@"600ml"];
//    standardClassInfo *tempClassInfo7 = [standardClassInfo StandardClassInfoWith:@"106" andStandClassName:@"700ml"];
//    standardClassInfo *tempClassInfo8 = [standardClassInfo StandardClassInfoWith:@"107" andStandClassName:@"800ml"];
//    standardClassInfo *tempClassInfo9 = [standardClassInfo StandardClassInfoWith:@"108" andStandClassName:@"900ml"];
//    
//    
//    NSArray *tempClassInfoArr = @[tempClassInfo1,tempClassInfo2,tempClassInfo3,tempClassInfo4,tempClassInfo5,tempClassInfo6,tempClassInfo7,tempClassInfo8,tempClassInfo9];
//    StandardModel *tempModel = [StandardModel StandardModelWith:tempClassInfoArr andStandName:@"选择型号"];
    
    
    
//    standardClassInfo *tempClassInfo10 = [standardClassInfo StandardClassInfoWith:@"109" andStandClassName:@"100ml"];
//    standardClassInfo *tempClassInfo11 = [standardClassInfo StandardClassInfoWith:@"110" andStandClassName:@"200ml"];
//    standardClassInfo *tempClassInfo12 = [standardClassInfo StandardClassInfoWith:@"111" andStandClassName:@"300ml"];
//    standardClassInfo *tempClassInfo13 = [standardClassInfo StandardClassInfoWith:@"112" andStandClassName:@"400ml"];
//    standardClassInfo *tempClassInfo14 = [standardClassInfo StandardClassInfoWith:@"113" andStandClassName:@"500ml"];
//    standardClassInfo *tempClassInfo15 = [standardClassInfo StandardClassInfoWith:@"114" andStandClassName:@"600ml"];
//    standardClassInfo *tempClassInfo16 = [standardClassInfo StandardClassInfoWith:@"115" andStandClassName:@"700ml"];
//    standardClassInfo *tempClassInfo17 = [standardClassInfo StandardClassInfoWith:@"116" andStandClassName:@"800ml"];
//    standardClassInfo *tempClassInfo18 = [standardClassInfo StandardClassInfoWith:@"117" andStandClassName:@"900ml"];
//    
//    NSArray *tempClassInfoArr2 = @[tempClassInfo10,tempClassInfo11,tempClassInfo12,tempClassInfo13,tempClassInfo14,tempClassInfo15,tempClassInfo16,tempClassInfo17,tempClassInfo18];
//    StandardModel *tempModel2 = [StandardModel StandardModelWith:tempClassInfoArr2 andStandName:@"选择型号"];
    
     
    
    
    
    
//    standardClassInfo *temp1 = [standardClassInfo StandardClassInfoWith:@[@"100",@"200",@"300",@"400",@"500",@"600",@"700",@"800",@"900",]];
//    standardClassInfo *temp2 = [standardClassInfo StandardClassInfoWith:@[@"100",@"200",@"300",@"400",@"500",@"600",@"700",@"800",@"900",]];
//    
//    StandardModel *tempModel1 = [StandardModel StandardModelWith:temp1 andStandName:@"选择品牌"];
//
//    StandardModel *tempModel2 = [StandardModel StandardModelWith:temp2 andStandName:@"选择型号"];

    //section个数
//    standview.standardArr = @[tempModel,tempModel2];
    
    
    
    
    return standview;
}

#pragma mark - standardView  delegate
//点击自定义按键
-(void)StandardsView:(StandardsView *)standardView CustomBtnClickAction:(UIButton *)sender BuyNum:(NSInteger)buyNum
{
    
    
    DLog(@"购买数量：%ld       ------- ",buyNum);
    if (standardView.tag == 1) {
         //加入购物车
//        if (_userInfo.good_standards == nil)
//        {
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"请您选择一下你所需要的产品规格" delegate:self cancelButtonTitle:@"知道啦" otherButtonTitles:nil, nil];
//            [alert show];
//        }
//        else
//        {
            //将商品图片抛到指定点
            [standardView ThrowGoodTo:CGPointMake(200, 100) andDuration:1.6 andHeight:150 andScale:20];
            
            
            //调用加入购物车网络接口
            
            
            _userInfo.good_num = [NSString stringWithFormat:@"%zd",buyNum];
//            _userInfo.good_standards = nil;
        
//        }
        
       
       
        
//        [standardView dismiss];
    }
    else
    {
        //立即购买
       
        
//        if (_userInfo.good_standards == nil)
//        {
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"请您选择一下你所需要的产品规格" delegate:self cancelButtonTitle:@"知道啦" otherButtonTitles:nil, nil];
//            [alert show];
//        }
//        else
//        {
            [standardView dismiss];
            
            //商品数量本地缓存
            _userInfo.good_num = [NSString stringWithFormat:@"%zd",buyNum];
            
            SubmitOrderViewController *svc = [[SubmitOrderViewController alloc]initWithNibName:@"SubmitOrderViewController" bundle:nil];
            svc.model = self.dataSource.lastObject;
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:svc animated:YES];
            

//        }
        
        
    }
}

//点击规格代理
-(void)Standards:(StandardsView *)standardView SelectBtnClick:(UIButton *)sender andSelectID:(NSString *)selectID andStandName:(NSString *)standName andIndex:(NSInteger)index
{
    //把规格存本地------
    
    DLog(@"selectID = %@ standName = %@  title = %@  index = %ld",selectID,standName,sender.titleLabel.text,(long)index);
    
    //商品规格缓存
//    _userInfo.good_standards = sender.titleLabel.text;//商品规格
    _userInfo.good_freight = @"10";//运费
    
    
    
}
//设置自定义btn的属性
-(void)StandardsView:(StandardsView *)standardView SetBtn:(UIButton *)btn
{
    if (btn.tag == 0) {
        btn.backgroundColor = [UIColor yellowColor];
    }
    else if (btn.tag == 1)
    {
        btn.backgroundColor = [UIColor orangeColor];
    }
}


#pragma mark --------- private Method



-(void)toTopAction:(UIButton *)sender
{
    DLog(@"回顶部");
    [self.detailTableView setContentOffset:CGPointMake(0, 0) animated:YES];
}

-(CGFloat)imgHeight:(UIImage *)img
{
    UIImage *im = [[UIImage alloc]init];
    im = LoadImage(@"testttt");
    DLog(@"%.f",im.size.height);
    return im.size.height;
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    //实例化本地缓存
    _userInfo = [CacheUserInfo sharedManager];
    _userInfo.good_num = nil;
//    _userInfo.good_standards = nil;
}
#pragma mark ----- lazy loading

-(GoodsDetailTopView *)topView
{
    WeakObj(self);
    if (!_topView) {
        _topView = [[GoodsDetailTopView alloc]init];
//        _topView.backgroundColor = [UIColor greenColor];
        [self.view addSubview:_topView];
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.top.equalTo(self.view.mas_top).offset(20);
            make.height.mas_offset(44);
        }];
        
        _topView.backButtonClickedHandler = ^(NSInteger buttonTag){
            
            if (buttonTag == 0)
            {
                [weakself.navigationController popViewControllerAnimated:YES];
            }
            else if (buttonTag == 1)
            {
                DLog(@"商品");
                
                weakself.bottomScrollView.contentOffset = CGPointMake(0, 0);
                [weakself.topView.scrollLine mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.left.equalTo(weakself.topView.detailButton.mas_left).offset(-112 * FitW);
                }];
                
            }
            else
            {
                weakself.bottomScrollView.contentOffset = CGPointMake(375 * FitW, 0);
                [weakself.topView.scrollLine mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.left.equalTo(weakself.topView.detailButton.mas_left);//.offset(-188 * FitW);
                }];
                
                DLog(@"详情");
            }
        };
    }
    return _topView;
}


-(UITableView *)tableView
{
    WeakObj(self);
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        [self.bottomScrollView addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(self.bottomScrollView.mas_left);
            make.top.equalTo(self.bottomScrollView.mas_top);
            //            make.bottom.equalTo(self.bottomScrollView.mas_bottom).offset(-57 * FitH);
            //            make.width.mas_equalTo(375 * FitW);
            make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 - 57 * FitH));
        }];
        
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"hh"];
        
        [_tableView registerNib:[UINib nibWithNibName:@"commentTableViewCell" bundle:nil] forCellReuseIdentifier:commentTableViewCellIden];
        [_tableView registerNib:[UINib nibWithNibName:@"CommentContentTableViewCell" bundle:nil] forCellReuseIdentifier:CommentContentTableViewCellIden];
        [_tableView registerNib:[UINib nibWithNibName:@"MoreBtnTableViewCell" bundle:nil] forCellReuseIdentifier:MoreBtnTableViewCellIden];
        [_tableView registerNib:[UINib nibWithNibName:@"GoodsDeatilTableViewCell" bundle:nil] forCellReuseIdentifier:GoodsDeatilTableViewCellIden];
        [_tableView registerClass:[GoodsScrollTableViewCell class] forCellReuseIdentifier:GoodsScrollTableViewCellIden];
        //
        _buyfoodsView = [[BuyGoodsView alloc]init] ;//]WithFrame:CGRectMake(0, 545 * FitH, 375 * FitW, 57 * FitH)];
        _buyfoodsView.backgroundColor = [UIColor whiteColor];
        [self.bottomScrollView addSubview:_buyfoodsView];
        [_buyfoodsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bottomScrollView.mas_left);
            make.top.equalTo(self.tableView.mas_bottom);
            make.right.equalTo(self.tableView.mas_right);
            make.height.mas_equalTo(57 * FitH);
        }];
        
        _buyfoodsView.buttonClickedHandler = ^(NSInteger buttonTag){
            
            DLog(@"%ld",buttonTag);
            if (buttonTag == 0)
            {
                DLog(@"立即购买");
                
                StandardsView *mystandardsView = [weakself buildStandardView:LoadImage(@"66.jpg") andIndex:buttonTag];
                mystandardsView.GoodDetailView = weakself.view;//设置该属性 对应的view 会缩小
                
                //
                mystandardsView.showAnimationType = StandsViewShowAnimationShowFromLeft;
                mystandardsView.dismissAnimationType = StandsViewDismissAnimationDisToRight;
                
                [mystandardsView show];
                
                //            weakself.hidesBottomBarWhenPushed = NO;
            }
            else if (buttonTag == 2)
            {
                if (weakself.flag == 0)
                {
                    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"收藏成功" delegate:weakself cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    
                    [weakself.buyfoodsView.collectBtn setBackgroundImage:LoadImage(@"collectshi") forState:UIControlStateNormal];
                    weakself.flag = 1 ;
                }
                else
                {
                    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"取消收藏成功" delegate:weakself cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                    [weakself.buyfoodsView.collectBtn setBackgroundImage:LoadImage(@"collectkong") forState:UIControlStateNormal];
                    weakself.flag = 0 ;
                }
                DLog(@"收藏");
                
                
                
            }
            else
            {
                //                [weakself.chooseGoodTypeView show];
                DLog(@"加入购物车");
                
                StandardsView *mystandardsView = [weakself buildStandardView:LoadImage(@"66.jpg") andIndex:buttonTag];
                mystandardsView.GoodDetailView = weakself.view;//设置该属性 对应的view 会缩小
                mystandardsView.delegate = weakself;
                //
                mystandardsView.showAnimationType = StandsViewShowAnimationShowFromLeft;
                mystandardsView.dismissAnimationType = StandsViewDismissAnimationDisToRight;
                
                [mystandardsView show];
                
            }
        };
        
    }
    return _tableView;
}

-(UITableView *)detailTableView
{
    if (!_detailTableView)
    {
        _detailTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
//        _detailTableView.backgroundColor = [UIColor greenColor];
        [self.bottomScrollView addSubview:_detailTableView];
        [_detailTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.tableView.mas_right);
            make.top.equalTo(self.tableView.mas_top);
            make.width.equalTo(self.tableView.mas_width);
            make.height.equalTo(@([UIScreen mainScreen].bounds.size.height - 64 ));
        }];
        _detailTableView.bounces = NO;
        _detailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _detailTableView.delegate = self;
        _detailTableView.dataSource = self;
        
        [_detailTableView registerNib:[UINib nibWithNibName:@"PromiseTableViewCell" bundle:nil] forCellReuseIdentifier:PromiseTableViewCellIden];
        [_detailTableView registerNib:[UINib nibWithNibName:@"GoodInfoTitleTableViewCell" bundle:nil] forCellReuseIdentifier:GoodInfoTitleTableViewCellIden];
        [_detailTableView registerNib:[UINib nibWithNibName:@"GoodInfoTableViewCell" bundle:nil] forCellReuseIdentifier:GoodInfoTableViewCellIden];
        [_detailTableView registerNib:[UINib nibWithNibName:@"GoodDetailImgTableViewCell" bundle:nil] forCellReuseIdentifier:GoodDetailImgTableViewCellIden];
        
        
//        UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 375, 500)];
//        [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
//        [self.detailTableView addSubview:web];
        
        
        
        
    }
    return _detailTableView;
}

//-(UIScrollView *)scrollView
//{
//    if (!_scrollView)
//    {
//        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(387 * FitW, 12 * FitH, 351 * FitW, 590 * FitH)];
//        _scrollView.showsVerticalScrollIndicator = NO;
//        _scrollView.scrollEnabled = YES;
//        _scrollView.bounces = NO;
//        _scrollView.backgroundColor = [UIColor whiteColor];
//        [_bottomScrollView addSubview:_scrollView];
////        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
////            make.left.equalTo(_bottomScrollView.mas_left).offset(self.view.frame.size.width);
////            make.right.equalTo(self.bottomScrollView.mas_right);
////            make.top.equalTo(self.bottomScrollView.mas_top);
////            make.bottom.equalTo(self.bottomScrollView.mas_bottom);
////        }];
//        
////        UIImageView *showImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 351 * FitW, 2652 * FitH)];
//////        showImage.image = LoadImage(@"商品详情页.jpg");
////        [showImage sd_setImageWithURL:[NSURL URLWithString:[self.dateImg.lastObject good_deatailImg]]];
////       
////        [_scrollView addSubview:showImage];
//        
//        _scrollView.contentSize = CGSizeMake(351 * FitW, 2652 * FitH);
////        [showImage mas_makeConstraints:^(MASConstraintMaker *make) {
////            make.edges.mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
////        }];
//    }
//    return _scrollView;
//}

- (YORequestHander *)requestHander
{
    //    WeakObj(self);
    if (!_requestHander) {
        _requestHander = [YORequestHander shareInstance];
        //        _requestHandler.netWorkNotReachableHandler = ^(){
        //            DLog(@"当前网络不可用");
        //            [weakself.noNetworkView show];
        //        };
    }
    return _requestHander;
}


@end
